package whu.o2o.adapter;

import java.util.ArrayList;

import whu.o2o.util.ConstantData;
import whu.o2o.util.Utils;
import whu.o2o.vo.CommentVO;
import whu.o2o.vo.PersonVO;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import whu.o2o.R;


public class DiaryCommentAdapter extends BaseAdapter {
	public ArrayList<CommentVO> publicList;
	private Context mContext;
	private Spanned mGoodIco;
	private int mPaddingSize;
	private float mTextSize;

	public DiaryCommentAdapter(Context context, ArrayList<CommentVO> imageList) {
		mContext = context;
		publicList = imageList;
		init();

	}

	private void init() {
		mGoodIco = Html.fromHtml("<img src=\"" + R.drawable.diary_good + "\">", new ImageGetter() {

			@Override
			public Drawable getDrawable(String source) {
				int id = Integer.parseInt(source);
				Drawable drawable = mContext.getResources().getDrawable(id);
				drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
				return drawable;
			}
		}, null);

		mPaddingSize = Utils.dip2px(mContext, 5);
		mTextSize = mContext.getResources().getDimension(R.dimen.text_small);
	}

	@Override
	public int getCount() {
		return publicList.size();
	}

	@Override
	public Object getItem(int position) {
		return publicList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {
		return publicList.get(position).getType();
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int type = getItemViewType(position);
		CommentVO commentVO = publicList.get(position);

		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();

			TextView textView = new TextView(mContext);
			textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
			textView.setPadding(mPaddingSize, mPaddingSize, mPaddingSize, mPaddingSize);

			switch (type) {
			case ConstantData.Data.GOOD_DIARY:
				textView.setTextColor(mContext.getResources().getColor(R.color.diary_good));
				break;

			case ConstantData.Data.COMMENT_DIARY:
				textView.setTextColor(mContext.getResources().getColor(R.color.diary_comment));
				break;
			}

			convertView = textView;
			holder.content = (TextView) convertView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		CharSequence content = null;

		switch (type) {
		case ConstantData.Data.GOOD_DIARY:
			content = parseGoodData(commentVO.getGoodList());
			break;

		case ConstantData.Data.COMMENT_DIARY:
			content = parseCommentData(commentVO);
			break;
		}

		if (!TextUtils.isEmpty(content)) {
			holder.content.setText(content);
		} else {
			holder.content.setText("");
		}

		return convertView;
	}

	/**
	 * 赞列表
	 */
	private SpannableStringBuilder parseGoodData(ArrayList<PersonVO> goodList) {
		SpannableStringBuilder builder = new SpannableStringBuilder();
		builder.append(mGoodIco);

		for (int i = 0; i < goodList.size(); i++) {
			PersonVO personVO = goodList.get(i);
			if (personVO != null && !TextUtils.isEmpty(personVO.getName())) {
				builder.append(personVO.getName());
				if (i < goodList.size() - 1) {
					builder.append("、");
				}
			}
		}
		return builder;
	}

	/**
	 * 评论列表
	 */
	private CharSequence parseCommentData(CommentVO commentVO) {
		SpannableStringBuilder builder = new SpannableStringBuilder();

		PersonVO personVO = commentVO.getUser();
		if (personVO != null && !TextUtils.isEmpty(personVO.getName())) {
			SpannableStringBuilder name = new SpannableStringBuilder(personVO.getName());
			name.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.diary_good)), 0, personVO
					.getName().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			builder.append(name);
			builder.append(": ");
			builder.append(commentVO.getContent());
		}

		return builder;
	}

	static class ViewHolder {
		TextView content;
	}
}
